package traj.dataset;

import qmo.geometry.Location;
import traj.core.Params;
import traj.core.TPoint;
import traj.core.TPoint.Direction;

/**
 * @author xiaohui
 * 
 *         This class records the various input formats of moving objects.
 *         Noticablly, the data format, from Brinkhoff's road network generator,
 *         is called NBG format.
 * 
 *         TPL format is : id loc_x loc_x loc_y loc_y<br>
 *         NBG format is: Behavior oid 1 0 0 loc_x loc_y road_id speed heading
 * 
 */
public class QmoMessage {
	final static String separator = "\t";

	public enum EntityType {
		OBJECT, QUERY
	}

	public enum Behavior {
		APPEAR, UPDATE, DISAPPEAR
	}

	EntityType entityType;

	Behavior behavior;
	int objectID;
	int roadID;
	double timestamp;
	Location location;
	float speed;
	TPoint.Direction direction;

	public QmoMessage(EntityType object, Behavior appe, int id,
			double timestamp, Location location, int roadID, float speed,
			Direction direction) {
		this.entityType = object;
		this.behavior = appe;
		this.objectID = id;
		this.timestamp = timestamp;
		this.location = location;
		this.roadID = roadID;
		this.speed = speed;
		this.direction = direction;
	}

	public String toString() {
		String s = this.entityType + separator + this.behavior + separator
				+ this.objectID + separator + this.timestamp + separator
				+ this.location;
		return s;
	}

	private String toNewpointString() {
		String line = Params.appear + separator + this.getId() + separator
				+ "1" + separator + "0" + separator + "0" + separator
				+ this.getLocation().x + separator + this.getLocation().y
				+ separator + roadID + separator + speed + separator
				+ direction;
		return line;
	}

	public String toTplString() {
		String s = "";
		s = s + this.objectID + separator;
		s = s + this.location.x + separator;
		s = s + this.location.x + separator;
		s = s + this.location.y + separator;
		s = s + this.location.y;
		return s;
	}

	// NBG String is the same format as generated by the Brinkhoff's road
	// network data generator.
	public String toNbgString() {
		switch (behavior) {
		case APPEAR:
			return toNewpointString();
		case UPDATE:
			return toUpdateString();
		case DISAPPEAR:
			return toDisappearString();
		default:
			System.err.println("Invalid moving object behaviour.");
			return null;
		}

	}

	// This method generates update messages
	private String toUpdateString() {
		return Params.update + separator + this.getId() + separator + "1"
				+ separator + "0" + separator + "0" + separator
				+ this.getLocation().x + separator + this.getLocation().y
				+ separator + roadID + separator + speed + separator
				+ direction;
	}

	private String toDisappearString() {
		return Params.disappear + separator + this.getId() + separator + "1"
				+ separator + "0" + separator + "0" + separator
				+ this.getLocation().x + separator + this.getLocation().y
				+ separator + roadID + separator + speed + separator
				+ direction;
	}

	public Behavior getBehavior() {
		return behavior;
	}

	public EntityType getEntityType() {
		return entityType;
	}

	public int getId() {
		return objectID;
	}

	public Location getLocation() {
		return location;
	}

	public double getTimestamp() {
		return timestamp;
	}

}
